#ifndef OMPL_GEOMETRIC_PLANNERS_RRT_HRRT_H
#define OMPL_GEOMETRIC_PLANNERS_RRT_HRRT_H

#include <ompl/geometric/planners/PlannerIncludes.h>
#include <ompl/datastructures/NearestNeighbors.h>

#include "hplanner_ompl/collision_estimator.h"

namespace ompl
{
namespace geometric
{

class hRRT : public base::Planner
{
public:

  typedef typename base::ApproxCSpaceOracleMultiprobe CSpaceOracle;
  typedef typename base::CollisionStatusLineEstimator CollisionStatusEstimator;


    /** \brief Constructor */
    hRRT(const base::SpaceInformationPtr &si) : base::Planner(si, "hRRT")
    {
        specs_.approximateSolutions = true;

        goalBias_ = 0.05;
        maxDistance_ = 0.0;

        cspaceOracle_ = NULL;
        collisionEstimator_ = NULL;

    }

    virtual ~hRRT(void)
    {
        freeMemory();

        delete cspaceOracle_; cspaceOracle_ = NULL;
        delete collisionEstimator_; collisionEstimator_ = NULL;

    }

    virtual void getPlannerData(base::PlannerData &data) const;

    virtual bool solve(const base::PlannerTerminationCondition &ptc);

    virtual void clear(void);

    /** \brief Set the goal bias

        In the process of randomly selecting states in
        the state space to attempt to go towards, the
        algorithm may in fact choose the actual goal state, if
        it knows it, with some probability. This probability
        is a real number between 0.0 and 1.0; its value should
        usually be around 0.05 and should not be too large. It
        is probably a good idea to use the default value. */
    void setGoalBias(double goalBias)
    {
        goalBias_ = goalBias;
    }

    /** \brief Get the goal bias the planner is using */
    double getGoalBias(void) const
    {
        return goalBias_;
    }

    /** \brief Set the range the planner is supposed to use.

        This parameter greatly influences the runtime of the
        algorithm. It represents the maximum length of a
        motion to be added in the tree of motions. */
    void setRange(double distance)
    {
        maxDistance_ = distance;
    }

    /** \brief Get the range the planner is using */
    double getRange(void) const
    {
        return maxDistance_;
    }

    /** \brief Set a different nearest neighbors datastructure */
    template<template<typename T> class NN>
    void setNearestNeighbors(void)
    {
        nn_.reset(new NN<Motion*>());
    }

    virtual void setup(void);

protected:


    /** \brief Representation of a motion

        This only contains pointers to parent motions as we
        only need to go backwards in the tree. */
    class Motion
    {
    public:

        Motion(void) : state(NULL), parent(NULL)
        {
        }

        /** \brief Constructor that allocates memory for the state */
        Motion(const base::SpaceInformationPtr &si) : state(si->allocState()), parent(NULL)
        {
        }

        ~Motion(void)
        {
        }

        /** \brief The state contained by the motion */
        base::State       *state;

        /** \brief The parent motion in the exploration tree */
        Motion            *parent;

    };

    /** \brief Free the memory allocated by this planner */
    void freeMemory(void);

    /** \brief Compute distance between motions (actually distance between contained states) */
    double distanceFunction(const Motion* a, const Motion* b) const
    {
        return si_->distance(a->state, b->state);
    }

    bool checkMotion(const base::State* s1, const base::State* s2, bool initgoal) const;

    /** \brief State sampler */
    base::StateSamplerPtr                          sampler_;

    /** \brief A nearest-neighbors datastructure containing the tree of motions */
    boost::shared_ptr< NearestNeighbors<Motion*> > nn_;

    /** \brief The fraction of time the goal is picked as the state to expand towards (if such a state is available) */
    double                                         goalBias_;

    /** \brief The maximum length of a motion to be added to a tree */
    double                                         maxDistance_;

    /** \brief The random number generator */
    RNG                                            rng_;

    CSpaceOracle* cspaceOracle_;
    base::CollisionStatusLineEstimator* collisionEstimator_;

};

}
}

#endif
